<?php
/*
 * @Author       : chaihao
 * @Date         : 2020-06-05 10:23:00
 * @FilePath     : /ai/controllers/api/UserCardController.php
 */

namespace app\controllers\api;

use app\commands\ErrorMsg;
use app\commands\Helper;
use app\commands\Validator;
use app\Services\api\UserCardService;
use Yii;

class UserCardController extends ApiController
{
    /**
     * 返回该用户名片样式
     */
    public function actionGetCardStyle()
    {
        $req = $this->req;
        $req['user_id'] = $req['userid'] ?? $this->user->ID;
        $res = UserCardService::getInstance()->getCardStyle($req);
        Helper::right_out($req, $res);
    }

    /**
     *  修改该用户名片样式为传参的style
     */
    public function actionSetCardStyle()
    {
        $req = $this->req;
        $rules = array(
            'style'  => 'required|integer',
        );
        Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        $req['user_id'] = $req['userid'] ?? $this->user->ID;
        $res = UserCardService::getInstance()->setCardStyle($req);
        Helper::right_out($req, $res);
    }

    /**
     * 返回该用户隐私设置
     */
    public function actionGetLimit()
    {
        $req = $this->req;
        $req['user_id'] = $req['userid'] ?? $this->user->ID;
        $res = UserCardService::getInstance()->getLimit($req);
        Helper::right_out($req, $res);
    }

    /**
     *  修改该用户隐私设置
     */
    public function actionSetLimit()
    {
        $req = $this->req;
        $req['user_id'] = $req['userid'] ?? $this->user->ID;
        $res = UserCardService::getInstance()->setLimit($req);
        Helper::right_out($req, $res);
    }
    /**
     * 查询用户模型
     *
     */
    public function actionCheckAvter()
    {
        $req = $this->req;
        $user_id = $req['userid'] ?? $this->user->ID;
        $res = UserCardService::getInstance()->checkAvter($user_id);
        Helper::right_out($req, $res);
    }

    /**
     * 设置用户模型
     */
    public function actionSetAvter()
    {
        $req = $this->req;
        $rules = array(
            'modid'  => 'required|integer',
        );
        Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        $user_id = $req['userid'] ?? $this->user->ID;
        $res = UserCardService::getInstance()->setAvter($user_id, $req['modid']);
        Helper::right_out($req, $res);
    }

    /**
     * 获取用户信息
     *
     * @return void
     */
    public function actionGetUserInfo()
    {
        $req = $this->req;
        $user_id = $req['userid'] ?? $this->user->ID;
        $res = UserCardService::getInstance()->getUserInfo($user_id);
        Helper::right_out($req, $res);
    }
    /**
     * 查询该uid绑定的邀请码信息
     *
     * @return void
     */
    public function actionGetIvtcode()
    {
        $req = $this->req;
        $user_id = $req['userid'] ?? $this->user->ID;
        $res = UserCardService::getInstance()->getIvtcode($user_id);
        Helper::right_out($req, $res);
    }

    /**
     * 设置user表中公司，职位（可能会有手机邮箱）全部都是可选参数
     *
     */
    public function actionSetUserInfo()
    {
        $req = $this->req;
        if (isset($req['email']) && !empty($req['email'])) {
            $rules = array(
                'email'  => 'email',
            );
            Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        }
        $req['user_id'] = $req['userid'] ?? $this->user->ID;
        $res = UserCardService::getInstance()->setUserInfo($req);
        Helper::right_out($req, $res);
    }


    /**
     * 获取发送名片记录
     *
     * @return void
     */
    public function actionGetCard()
    {
        $req = $this->req;
        $res = UserCardService::getInstance()->getSendCard($req);
        Helper::right_out($req, $res);
    }
    /**
     * 添加发送名片记录
     *
     * @return void
     */
    public function actionSendCard()
    {
        $req = $this->req;
        $rules = array(
            'from_id'  => 'required|integer',
            'to_id'  => 'required|integer',
            'type'  => 'required|integer',
            'name'  => 'required',
            'updatetime'  => 'required',
        );
        if (isset($req['email']) && !empty($req['email'])) {
            $rules = array(
                'email'  => 'email',
            );
        }
        Validator::validator($req, $rules, ErrorMsg::ERROR_MSG);
        $res = UserCardService::getInstance()->setSendCard($req);
        Helper::right_out($req, $res);
    }
    /**
     * 重置邀请码过期会员信息
     *
     */
    public function actionUpdateIvtCode()
    {
        $req = $this->req;
        $res = UserCardService::getInstance()->updateIvtCode($req);
        Helper::right_out($req, $res);
    }
}
